﻿using ImoviesBlazorAntPro.Server.Data;
using ImoviesBlazorAntPro.Shared.Entities;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ImoviesBlazorAntPro.Server.Repository
{
    public class TeleplayRepository : ITeleplayRepository
    {
        private readonly ApplicationDbContext _dbContext;

        public TeleplayRepository(ApplicationDbContext dbContext)
        {
            this._dbContext = dbContext;
        }


        /// <summary>
        /// 获取全部数据，带年份
        /// </summary>
        /// <returns></returns>
        public Task<IEnumerable<Teleplay>> GetAllListAsync()
        {
            return Task.FromResult(_dbContext.Set<Teleplay>().OrderByDescending(x => x.Year).AsEnumerable());
        }




        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageNum"></param>
        /// <returns></returns>
        public Task<IEnumerable<Teleplay>> GetListAsync(int pageSize, int pageNum)
        {
            return Task.FromResult(_dbContext.Set<Teleplay>()
                .OrderByDescending(x => x.Year)
                .Skip((pageNum - 1) * pageSize)
                .Take(pageSize)
                .AsEnumerable());
        }


        /// <summary>
        /// 分页获取纯列表
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageNum"></param>
        /// <returns></returns>
        public Task<IEnumerable<Teleplay>> GetPureListAsync(int pageSize, int pageNum)
        {
            return Task.FromResult(_dbContext.Set<Teleplay>()
                .Skip((pageNum - 1) * pageSize)
                .Take(pageSize)
                .AsEnumerable());
        }


        /// <summary>
        /// 获取视频播放链接列表
        /// </summary>
        /// <param name="vid"></param>
        /// <returns></returns>
        public Task<IEnumerable<TeleplayVlink>> GetVlinkList(string vid)
        {
            return Task.FromResult(_dbContext.Set<TeleplayVlink>()
            .Where(x => x.TeleplayId == vid)
            .AsEnumerable());
        }


        /// <summary>
        /// 查询所有年份
        /// </summary>
        /// <returns></returns>
        public IEnumerable<uint> GetAllYears()
        {
            return _dbContext.Set<Teleplay>()
                .Select(x => x.Year).AsEnumerable().Distinct();
        }


        /// <summary>
        /// 获取总记录数
        /// </summary>
        /// <returns></returns>
        public int GetTotal()
        {
            return _dbContext.Set<Teleplay>().Count();
        }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="teleplay"></param>
        public void AddTeleplay(Shared.Entities.Teleplay teleplay)
        {
            _dbContext.Set<Shared.Entities.Teleplay>().Add(teleplay);
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="teleplay"></param>
        public void UpdateTeleplay(Shared.Entities.Teleplay teleplay)
        {
            _dbContext.Set<Shared.Entities.Teleplay>().Update(teleplay);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="teleplayId"></param>
        public async Task DeleteTeleplay(string teleplayId)
        {
            var a = await _dbContext.Set<Shared.Entities.Teleplay>().FindAsync(teleplayId);
            if (a != null)
            {
                _dbContext.Set<Shared.Entities.Teleplay>().Remove(a);
            }
        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        public void DeleteBatch(List<string> ids)
        {
            var list = _dbContext.Set<Shared.Entities.Teleplay>().Where(x => ids.Contains(x.Id)).ToList();
            if (list.Count > 0)
            {
                _dbContext.Set<Shared.Entities.Teleplay>().RemoveRange(list);
            }
        }


        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        public bool Save()
        {
            return _dbContext.SaveChanges() > 0;
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public IEnumerable<Teleplay> Search(string keyword)
        {
            return _dbContext.Set<Shared.Entities.Teleplay>().Where(x => x.Name.Contains(keyword)).AsEnumerable();
        }
    }
}
